package com.letv.android.client.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

/**
 * 所有fragment的基类
 * 
 * @author zhuqiao
 *
 */
public abstract class LetvBaseFragment extends Fragment implements LetvFragmentListener {
    protected Context mContext;
    protected View mContentView;
    /** 是否已经初始化完成 **/
    protected boolean mHasInited;

    private OnFragmentStateCallback mCallback;

    public interface OnFragmentStateCallback {

        void inited();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mHasInited = true;
        if (mCallback != null) {
            mCallback.inited();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected <T extends View> T getViewById(int id) {
        return getViewById(mContentView, id);
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T getViewById(View parent, int id) {
        if (mContentView != null) {
            return (T) parent.findViewById(id);
        }

        return null;
    }

    public View getContentView() {
        return mContentView;
    }

    public void setCallback(OnFragmentStateCallback callback) {
        mCallback = callback;
    }

    /**
     * 显示
     */
    @Override
    public void onShow() {
    }

    /**
     * 隐藏
     */
    @Override
    public void onHide() {
    }

    @Override
    public int getDisappearFlag() {
        return HIDE;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (mCallback != null && !hidden) {
            mCallback.inited();
        }
    }

}
